package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.UserDao;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<User> findAllUserByPage(UserVo userVo) {
        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());
        List<User> list = userDao.findAllUserByPage(userVo);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void updateUserStatus(Integer id, String status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userDao.updateUserStatus(user);
    }

    @Override
    public User login(String phone, String password) throws Exception {
        User user = new User();
        user.setPhone(phone);
        User user1 = userDao.login(user);
        if(user1 != null && Md5.verify(password, Md5.md5key, user1.getPassword())){
            return user1;
        } else {
            return null;
        }
    }

    @Override
    public List<Role> findUserRoleById(int userId) {
        List<Role> list = userDao.findUserRoleById(userId);
        return list;
    }

    @Override
    public void userContextRole(UserRoleVo userRoleVo) {
        int userId = userRoleVo.getUserId();
        userDao.deleteUserContextRole(userId);
        for(Integer roleId : userRoleVo.getRoleIdList()){
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userId);
            user_role_relation.setRoleId(roleId);
            Date date = new Date();
            user_role_relation.setCreatedTime(date);
            user_role_relation.setUpdatedTime(date);
            user_role_relation.setCreatedBy("system");
            user_role_relation.setUpdatedBy("system");
            userDao.userContextRole(user_role_relation);
        }
    }

    @Override
    public ResponseResult getUserPermissions(Integer id) {
        //1. 获取当前用户的角色
        List<Role> roleList = userDao.findUserRoleById(id);
        //2. 将所有角色id封装成结合
        List<Integer> roleIdList = new ArrayList<>();
        for(Role role : roleList){
            roleIdList.add(role.getId());
        }
        //3. 按照角色查询顶级菜单
        List<Menu> menuList = userDao.findParentMenuByRoleId(roleIdList);
        //4. 查询子菜单
        for(Menu menu : menuList){
            List<Menu> subMenuList = userDao.findSubMenuListByPid(menu.getId());
            menu.setSubMenuList(subMenuList);
        }
        //5. 查询资源
        List<Resource> resourceList = userDao.findResourceByRoleId(roleIdList);
        //6. 封装响应结果
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", menuList);
        map.put("resourceList", resourceList);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", map);
        return responseResult;
    }
}
